package cn.css0209.applicationdemo190509.User;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.json.JSONException;
import org.json.JSONObject;

import cn.css0209.applicationdemo190509.MenuActivity;
import cn.css0209.applicationdemo190509.R;
import cn.css0209.applicationdemo190509.utils.OkHttp;
import cn.css0209.applicationdemo190509.utils.StatusBarUtil;

public class UserInfoActivity extends Activity {
    private EditText userName_ed, Phone_ed, Location_ed, specificAddress_ed;
    private RadioGroup sex_rg;
    private RadioButton man_rb, woman_rb, other_rb;
    private Button submitBtn_B;

    private String Sex_ch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        //        创建okHttp对象
        OkHttp okHttp = new OkHttp(this);
        okHttp.infoUser();

        //初始化
        init();

    }

    private void init() {
//        组件初始化
        userName_ed = findViewById(R.id.info_name_edit);
        Phone_ed = findViewById(R.id.info_phone_edit);
        Location_ed = findViewById(R.id.info_location_edit);
        specificAddress_ed = findViewById(R.id.info_specific_address_edit);
        sex_rg = findViewById(R.id.info_sexGroup);
        man_rb = findViewById(R.id.info_Man);
        woman_rb = findViewById(R.id.info_Woman);
        other_rb = findViewById(R.id.info_Other);
        submitBtn_B = findViewById(R.id.info_submit);

//        获取sessionID，确认登录状态
        final SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        String sessionId = sharedPreferences.getString("session", "");
        if (sessionId == null) {
            Intent it = new Intent(this, MainActivity.class);
            this.startActivity(it);
            this.finish();
        } else if (sessionId.isEmpty()) {
            Intent it = new Intent(this, MainActivity.class);
            this.startActivity(it);
            this.finish();
        }

//        填入用户信息
        String userName = sharedPreferences.getString("userName", "");
        String Phone = sharedPreferences.getString("Phone", "");
        final String Sex = sharedPreferences.getString("Sex", "other");
        String Location = sharedPreferences.getString("Location", "");
        String specificAddress = sharedPreferences.getString("specificAddress", "");
        userName_ed.setText(userName);
        Phone_ed.setText(Phone);
        if (Sex.equals("man")) {
            sex_rg.check(man_rb.getId());
        } else if (Sex.equals("woman")) {
            sex_rg.check(woman_rb.getId());
        } else {
            sex_rg.check(other_rb.getId());
        }
        Sex_ch = Sex;
        Location_ed.setText(Location);
        specificAddress_ed.setText(specificAddress);
        sex_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == man_rb.getId()) {
                    Sex_ch = "man";
                } else if (checkedId == woman_rb.getId()) {
                    Sex_ch = "woman";
                } else if(checkedId == other_rb.getId()){
                    Sex_ch = "other";
                } else {
                    Sex_ch = sharedPreferences.getString("Sex","other");
                    System.out.println("Sex_ch1: "+Sex_ch);
                }
            }
        });
        topBar();
    }

    public void info_update(View view) {
        String userName = userName_ed.getText().toString();
        String Phone = Phone_ed.getText().toString();
        String Location = Location_ed.getText().toString();
        String specificAddress = specificAddress_ed.getText().toString();


        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("Phone", Phone);
            jsonObject.put("location", Location);
            jsonObject.put("specificAddress", specificAddress);
            jsonObject.put("Sex", Sex_ch);
            System.out.println("Sex: " + Sex_ch);
            OkHttp okHttp = new OkHttp(this);
            okHttp.update(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            Intent intent = new Intent(this, MenuActivity.class);
            startActivity(intent);
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //    沉浸状态栏
    public void topBar() {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, false)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x33000000);
        }
    }

}
